import { Component, OnInit } from '@angular/core';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { ApiService } from 'src/app/core/api';
import { MessageService } from 'src/app/core/service/message.service';
import { FormBuilder, Validators } from '@angular/forms';
import { DatePipe } from '@angular/common';
import { Router } from '@angular/router';
import { I18nService } from 'src/app/core/i18n/i18n.service';

@Component({
	templateUrl: './index.html',

	providers: [DatePipe],
})
export class GuideMgmtComponent implements OnInit {
	page: any = {
		pageNo: 1,
	};
	travelAgencyList = [];
	dateRange;
	dataList = [];
	tableLoading = false;
	selectedRowData: any = {};
	validateForm;
	placeholder = {};
	modal:any = {};
	constructor(private i18n: I18nService,private apiService: ApiService, private nzMessageService: NzMessageService, private message: MessageService, private fb: FormBuilder, private datepipe: DatePipe, private router: Router, private modalService: NzModalService) {
		this.i18n.get(['modal','placeholder','pagetitle']).subscribe((res:any)=>{
			this.modal =res.modal;
			this.placeholder = res.placeholder;
			const pagetitle = res.pagetitle;
			this.message.setBreadcrumb({
				breadcrumbs: [{ name: pagetitle.title2}],
			});
		})
	}
	ngOnInit() {
		this.apiService.post('旅游社列表(用于选择框)', {}).then((res: any) => {
			this.travelAgencyList = res.data.list;
			this.tableLoading = false;
		});
		this.loadList();
	}
	loadList() {
		this.tableLoading = true;
		this.apiService.post('导游列表', { ...this.page }).then((res: any) => {
			this.dataList = res.data.list;
			this.page.pageNo = res.data.pageNo;
            this.page.pages = res.data.pages * 10;
			this.tableLoading = false;
		});
	}
	changeCurrentPage(e) {
        if (e) {
            this.page.pageNo = e;
            this.loadList();
        }
    }
	onChange(date) {
		if (date.length > 0) {
			this.page.beginTime = this.datepipe.transform(date[0], 'yyyy-MM-dd');
			this.page.endTime = this.datepipe.transform(date[1], 'yyyy-MM-dd');
		} else {
			this.page.beginTime = '';
			this.page.endTime = '';
		}
	}
	searchList() {
		this.page.pageNo = 1;
		this.loadList();
	}
	confirmStatus(status,id){
		const text = status === 'Y' ? this.modal.guideModal1 : this.modal.guideModal2;
		this.modalService.confirm({
			nzTitle: this.modal.title,
			nzContent: text,

			nzOnOk: () => {
				this.changeStatus(status, id);
			}
		});
	}
	changeStatus(status, id) {
		if (status === 'Y') {
			this.apiService.post('服务上下架', { id, status }).then(res => {
				this.modalService.success({
					nzTitle: this.modal.title,
					nzContent: this.modal.guideModal3,
				});
				this.loadList();
			});
		} else {
			this.apiService.post('查询服务产品是否激活', { guideId: id, status: 'Y' }).then((res: any) => {
				if (res.data.flag === 'Y') {
					this.modalService.warning({
						nzTitle: this.modal.title,
						nzContent: this.modal.guideModal4,
					});
				} else {
					this.apiService.post('服务上下架', { id, status }).then(res => {
						this.nzMessageService.success(this.modal.guideModal5);
						this.loadList();
					});
				}
			});
		}
	}
}
